package com.example.sunsg.okhttptest.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.breadtrip.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class EventBusSecondActivity extends AppCompatActivity {

    public static void launch(Context context){
        context.startActivity(new Intent(context,EventBusSecondActivity.class));
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_bus_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                EventBus.getDefault().post(new EventBusActivity.EventMessage("titlettttt","contentccccc"));
            }
        });
        Log.i("test","oncreate1");
        registerEvent();
        Log.i("test","oncreate2");
    }

    private void registerEvent(){
        EventBus.getDefault().register(this);
        EventBus.getDefault().clearCaches();
    }

    private void unregisterEvent(){
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().removeAllStickyEvents();
    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void eventBusMethed(SecondMessage message){
        Log.i("test","message === "+message.getName() +" age = "+message.getAge());
        Log.i("test","message.tostring = "+message.toString());
        EventBus.getDefault().removeStickyEvent(message);
    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void eventBusMethed(String message){
        Log.i("test", "message.tostring = " + message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterEvent();
    }

    public static class SecondMessage{
        private String name;
        private String age;

        public SecondMessage() {
        }

        public SecondMessage(String name, String age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }
    }

}
